<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件


/*
 * 常用ajax返回数据
 */
function ajaxMsg($status,$msg)
{
    $data['status'] = $status;
    $data['msg'] = $msg;
    return json($data);
}


//上传文件调用该函数
// location == 1  上传到七牛
//参数        无缩略图      file
//            有缩略图      file,width,height
//location == 2   上传到本地
//参数         无缩略图      file,width=null,height=null,path(此处应传入width=null,height=null,如不穿入这两个参数,则这图片不会上传)
//             有缩略图      file,width,height,path(path必须开头请添加添加'/')
function uploadFile($file,$width=null,$height=null,$path=null){
    $location = \think\Config::get('setting.img_upload_location');
    //此处上传到七牛
    if($location == 1){
        $qiniu = new \QiniuUpload\QiNiuUpload();
        if(empty($width) || empty($height)){
            return $qiniu->uploadPic($file);
        }else{
            return $qiniu->uploadThumb($file,$width,$height);
        }
    }
    //此处上传到本地服务器
    if($location == 0){
        if(empty($path)){
            return false;
        }
        return imgUpload($file,$width,$height,$path);
    }
}

//封装删除本地和七牛上的图片
function uploadDelete($arr = [],$origin=1)
{
    if($origin == 0){
        foreach($arr as $v){
            $v = ltrim($v,'/');
            if(is_file($v)){
                unlink($v);
            }
        }
    }else if($origin == 1){
        foreach($arr as $v){
            $qiniu = new \QiniuUpload\QiNiuUpload();
            $qiniu->deleteFile($v);
        }
    }else{
        return ['error'=>1,'msg'=>'未知图片来源'];
    }
}

//此处$arr是一个二维数组[[0=>'img_url',1=>'img_origin'],[0=>'',1=>'']]
function uploadDeleteFile($arr = []){
    $qiniu = new \QiniuUpload\QiNiuUpload();
    foreach($arr as $k=>$v){
        if($v[1] == 1){
            $qiniu->deleteFile($v[0]);
        }else if($v[1] == 0){
            $v[0] = ltrim($v[0],'/');
            if(is_file($v[0])){
                unlink($v[0]);
            }
        }else{
            return ['error'=>1,'msg'=>'未知图片来源'];
        }
    }
}

//图片上传到本地的处理
function imgUpload($file,$width,$height,$path)
{
    $file = request()->file($file);
    if(!$file){
        return false;
    }
    if(empty($path)){
        $path = "/uploads/images";
    }
    if(!is_dir($path)){
        mkdir($path);
    }
    $info = $file->move($path);
    if($info){
        $pathImg = str_replace('\\','/',$path.'/'.$info->getSaveName());
        if(empty($width) || empty($height)){
            return ['origin'=>'/'.$pathImg,'tag'=>0];
        }else{
            $thumbPath = pathinfo($pathImg)['dirname'].'/thumb';
            if(!is_dir($thumbPath)){
                mkdir($thumbPath);
            }
            $image = \think\Image::open($pathImg);
            $thumbFileName = $thumbPath.'/'.$info->getFileName();
            $image->thumb($width,$height)->save($thumbFileName);
            return ['origin'=>'/'.$pathImg,'thumb'=>'/'.$thumbFileName,'tag'=>0];
        }
    }
}

/**
 * @return string
 * 获取今天到今天结束的比较时间
 *
 */
function get_time_com(){
    $t = time();
    $start = mktime(0, 0, 0, date("m", $t), date("d", $t), date("Y", $t));
    $end = mktime(23, 59, 59, date("m", $t), date("d", $t), date("Y", $t));
    $start = date('Y-m-d H:i:s', $start);
    $end = date('Y-m-d H:i:s', $end);
    $where = "create_time >= '$start' and create_time <= '$end'";
    return $where;
}

/**
 * @return string
 * 获取的当前的域名
 */
function getHostDomain()
{
    // return $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['SERVER_NAME'];
//    return getHttpType() . $_SERVER['SERVER_NAME'];
    return 'https://'. $_SERVER['SERVER_NAME'];
}

/**
 * 获取 HTTPS协议类型
 * @return string
 */
function getHttpType()
{
    return $type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';
}

/*
 * 获取到当前主机IP
 */
function get_server_ip()
{
    if (isset($_SERVER['SERVER_NAME'])) {
        return gethostbyname($_SERVER['SERVER_NAME']);
    } else {
        if (isset($_SERVER)) {
            if (isset($_SERVER['SERVER_ADDR'])) {
                $server_ip = $_SERVER['SERVER_ADDR'];
            } elseif (isset($_SERVER['LOCAL_ADDR'])) {
                $server_ip = $_SERVER['LOCAL_ADDR'];
            }
        } else {
            $server_ip = getenv('SERVER_ADDR');
        }
        return $server_ip ? $server_ip : '获取不到服务器IP';
    }
}

//获取缓存小程序配置文件
function getWxCache(){
    //查询缓存
    $cache = \think\Cache::get('wxconfig');
    //缓存不存在
    if(!$cache){
        //重新查询存储到缓存
        $result = \app\admin\model\Wxsetting::find();
        $result = $result->toArray();
        \think\Cache::set('wxconfig',$result);
    }
    return \think\Cache::get('wxconfig');
}

